from starlette.endpoints import HTTPEndpoint
from starlette.responses import JSONResponse
from model.ModPendingProSupType import PendingProSupType, pendingProSupTypeSchema
from model.ModPendingProSupTypeBureau import PendingProSupTypeBureau,pendingProSupTypeBureauSchema
from database import Session, engine
import datetime


def get_pending_pro_sup_type(session, bureauid: str, stationid: str):
    # 待处理问题统计表故障类
    connection = engine.raw_connection()
    try:
        cursor = connection.cursor()
        cursor.callproc("statics_pending_pro_stu_type")
        cursor.close()
        connection.commit()
        if stationid == "00":
            pending_pro = session.query(PendingProSupTypeBureau).filter(PendingProSupTypeBureau.bureauid == bureauid).order_by(PendingProSupTypeBureau.pending_number.desc()).all()
            return JSONResponse(pendingProSupTypeBureauSchema.dump(pending_pro, many=True).data)
        else:
            pending_pro = session.query(PendingProSupType).filter(PendingProSupType.bureauid == bureauid).filter(
                PendingProSupType.stationid == stationid).order_by(PendingProSupType.pending_number.desc()).all()
            return JSONResponse(pendingProSupTypeSchema.dump(pending_pro, many=True).data)
    finally:
        connection.close()
